package cz.movie.subRenamer.model;

import java.io.File;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import cz.movie.subRenamer.util.FileNameUtil;

public class FileRepresentation {
	public static final String EPISODE_PLACEHOLDER = "$EPISODE$";
	public static final String SEASON_PLACEHOLDER = "$SEASON$";
	public static final String NAME_PLACEHOLDER = "$NAME$";
	private static final String EPISODE_PLACEHOLDER_QUOTE = Pattern.quote(EPISODE_PLACEHOLDER);
	private static final String SEASON_PLACEHOLDER_QUOTE = Pattern.quote(SEASON_PLACEHOLDER);
	private static final String NAME_PLACEHOLDER_QUOTE = Pattern.quote(NAME_PLACEHOLDER);

	public static final Set<String> playable = new HashSet<String>();
	static {
		playable.add("avi");
		playable.add("mp4");
		playable.add("wmv");
		playable.add("mkv");
		playable.add("flv");
		playable.add("3gp");
		playable.add("mov");
		playable.add("mpe");
		playable.add("mpg");
		playable.add("mpeg");
	}

	public static final Set<String> subtitleExtensions = new HashSet<String>();
	static {
		subtitleExtensions.add("txt");
		subtitleExtensions.add("sub");
		subtitleExtensions.add("srt");
	}

	private int episodeNumber;
	private int season;
	private String episodeSpecial;
	private boolean playableExtension;
	private boolean subtitleExtension;

	private String language;
	private String name;
	private String extension;
	private File file;
	private List<FileRepresentation> subtitles = new LinkedList<FileRepresentation>();

	public int getEpisodeNumber() {
		return episodeNumber;
	}

	public void setEpisodeNumber(int episode) {
		this.episodeNumber = episode;
	}

	public int getSeason() {
		return season;
	}

	public void setSeason(int season) {
		this.season = season;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getExtension() {
		return extension;
	}

	public void setExtension(String extension) {
		playableExtension = playable.contains(extension.toLowerCase());
		subtitleExtension = subtitleExtensions.contains(extension.toLowerCase());
		this.extension = extension;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public boolean hasPlayableExtension() {
		return playableExtension;
	}

	public boolean hasSubtitleExtension() {
		return subtitleExtension;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getEpisode() {
		return (episodeSpecial != null ? episodeSpecial : String.valueOf(getEpisodeNumber()));
	}

	public void setEpisodeSpecial(String episodeSpecial) {
		this.episodeSpecial = episodeSpecial;
	}

	public List<FileRepresentation> getSubtitles() {
		return subtitles;
	}

	public void setSubtitles(List<FileRepresentation> subtitles) {
		this.subtitles = subtitles;
	}

	@Override
	public String toString() {
		return "FileRepresentation [episode=" + getEpisode() + ", season=" + season + ", canPlay=" + playableExtension + ", language=" + language + ", name=" + name + ", extension=" + extension + ", playableFile=" + file + ", subtitles=" + subtitles + "]";
	}

	public String getStringRepresentation() {
		return getSeason() + "_" + getEpisode();
	}

	public String createNameForPattern(String output) {
		return output.replaceAll(EPISODE_PLACEHOLDER_QUOTE, FileNameUtil.addZeroPrefix(2, getEpisode())).replaceAll(SEASON_PLACEHOLDER_QUOTE, FileNameUtil.addZeroPrefix(2, String.valueOf(getSeason()))).replaceAll(NAME_PLACEHOLDER_QUOTE, getName()) + "." + getExtension();
	}

}
